package com.carlyang.jspservlet.util;

import java.sql.Connection;
import java.sql.DriverManager;


public class DbUtil {
	private String dbUrl = "jdbc:mysql://localhost:3306/db_jsp";
	private String dbUserName = "carlyang";
	private String dbPassword = "yangdong";
	private String jdbcName = "com.mysql.jdbc.Driver";
	
	public Connection getCon()throws Exception {
		Class.forName(jdbcName);
		Connection connection = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
		return connection;
	}
	
	public void closeCon(Connection connection)throws Exception {
		if (connection!=null) {
			connection.close();
		}
	}
	
	public static void main(String[] args) {
		DbUtil dbUtil = new DbUtil();
		Connection con =null;
		try {
			con = dbUtil.getCon();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (con!=null) {
					System.out.println("测试数据库连接成功");
					dbUtil.closeCon(con);
					System.out.println("关闭数据库");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
